//
//  RBDZVIewAddScroll.m
//  rubaoduanzi
//
//  Created by 李云飞 on 16/9/16.
//  Copyright © 2016年 liyunfei. All rights reserved.
//
/**头像的个数*/
#define Num 7
/**头像的宽度*/
#define W 42
/**头像离view顶部的距离*/
#define Top 10
/**每页的总数*/
#define Count 28
/**轮播视图的页数*/
#define NumView 2

#import "RBDZVIewAddScroll.h"
#import "RBDZEmojisModel.h"
@implementation RBDZVIewAddScroll
-(NSMutableArray *)arrayM{
    if (_arrayM==nil) {
        _arrayM=[NSMutableArray array];
        NSArray *array=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"emojis" ofType:@"plist"]];
        for (NSDictionary *dict in array) {
            RBDZEmojisModel *model=[RBDZEmojisModel EmojisWithDict:dict];
            [_arrayM addObject:model];
        }
    }
    return _arrayM;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.backgroundColor=[UIColor lightGrayColor];
        _scrollView=[[UIScrollView alloc]initWithFrame:frame];
        _scrollView.delegate=self;
        UIView *view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        CGFloat h=W;
        CGFloat mar=(_scrollView.frame.size.width-W*Num)/(Num+1);
        for (int i=0; i<Count-1; i++) {
            RBDZEmojisModel *model=self.arrayM[i];
            UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(mar+i%Num*(W+mar), Top+i/Num*(h+mar), W, h)];
            img.image=[UIImage imageNamed:[NSString stringWithFormat:@"%@",model.EmojiPng]];
            img.userInteractionEnabled=YES;
            img.tag=i;
            [view1 addSubview:img];
            // NSLog(@"%@",img);
        }
        UIButton *button1=[[UIButton alloc] initWithFrame:CGRectMake(mar+(Num-1)*(mar+W), Top+(Count/Num-1)*(mar+h), W, h)];
        [button1 setBackgroundImage:[UIImage imageNamed:@"list_bqsc"] forState:UIControlStateNormal];
        [button1 setBackgroundImage:[UIImage imageNamed:@"list_bqsc_pre"] forState:UIControlStateHighlighted];
        [view1 addSubview:button1];
        UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(_scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height)];
        int j=0;
        for (; j<1; j++) {
            RBDZEmojisModel *model=self.arrayM[j+Count-1];
            UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(mar+j%Num*(W+mar), Top+j/Num*(h+mar), W, h)];
            img.image=[UIImage imageNamed:model.EmojiPng];
            img.tag=j+Count-1;
            [view2 addSubview:img];
        }
        UIButton *button2=[[UIButton alloc] initWithFrame:CGRectMake(mar+j%Num*(mar+W), Top+j/Num*(mar+h), W, h)];
        [button2 setBackgroundImage:[UIImage imageNamed:@"list_bqsc"] forState:UIControlStateNormal];
        [button2 setBackgroundImage:[UIImage imageNamed:@"list_bqsc_pre"] forState:UIControlStateHighlighted];
        [view2 addSubview:button2];
        //NSLog(@"%@",_view1.subviews);
        [_scrollView addSubview:view1];
        [_scrollView addSubview:view2];
        _scrollView.contentSize=CGSizeMake(_scrollView.frame.size.width*NumView, 0);
        [self addSubview:_scrollView];
        _pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 40, 37)];
        _pageControl.numberOfPages=NumView;
        _pageControl.center=CGPointMake(self.scrollView.frame.size.width*0.5, self.scrollView.frame.size.height-15);
        _pageControl.pageIndicatorTintColor=[UIColor grayColor];
        _pageControl.currentPageIndicatorTintColor=[UIColor whiteColor];
        [self addSubview:_pageControl];
        self.scrollView.pagingEnabled=YES;
        self.scrollView.bounces=NO;
        self.scrollView.showsHorizontalScrollIndicator=NO;
    }
    return self;
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [UIView animateWithDuration:0.5 animations:^{
        NSInteger number=self.scrollView.contentOffset.x/self.scrollView.frame.size.width;
        if (number==NumView-1) {
            number=0;
        }else{
            number++;
        }
        
        self.pageControl.currentPage=number;
        self.scrollView.contentOffset=CGPointMake(self.scrollView.frame.size.width*number, 0);
    } completion:nil];

}
@end
